package main.reports;

import main.FactageApp;
import org.jdesktop.swingx.JXBusyLabel;
import javax.swing.*;
import java.awt.*;

/**
 * Created by a_lyulyasov on 04.06.14.
 */

public class BusyDialog extends Thread {

    //region Объявление компонентов формы
    private JXBusyLabel BusyIcon;
    private JLabel DescriptionLabel;
    private JButton DoneButton;
    private JDialog busyDlg;
    //endregion

    private String caption, description;

    //region Конструктор класса
    public BusyDialog(String caption, String description) {
        this.caption = caption;
        this.description = description;
    }
    //endregion

    //region run() - запуск потока
    public void run() {
        initComponents();
        int width = 400;
        int height = 200;
        busyDlg.setSize(new Dimension(width, height));
        Dimension screensize = Toolkit.getDefaultToolkit().getScreenSize();
        busyDlg.setLocation(screensize.width / 2 - width / 2, screensize.height / 2 - height / 2);
        busyDlg.setVisible(true);

        BusyIcon.setBusy(true);
        DoneButton.setEnabled(false);
    }
    //endregion

    //region stopBusy() - Остановка потока
    public void stopBusy() {
        BusyIcon.setBusy(false);
        BusyIcon.setVisible(false);
        DoneButton.setEnabled(true);
    }
    //endregion

    //region setDescription(String descr) - изменение текста диалогового окна
    public void setDescription(String descr) {
        if (busyDlg != null && busyDlg.isVisible()) {
            DescriptionLabel.setText(descr);
            busyDlg.pack();
        }
    }
    //endregion

    //region initComponents() - Инициализация компонентов
    private void initComponents() {
        JPanel jPanel1 = new JPanel();
        JLabel captionLabel = new JLabel(caption);
        BusyIcon = new org.jdesktop.swingx.JXBusyLabel();
        DescriptionLabel = new javax.swing.JLabel(description);
        DoneButton = new javax.swing.JButton();
        busyDlg = new JDialog(FactageApp.ma, true);

        busyDlg.setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
        busyDlg.setType(java.awt.Window.Type.UTILITY);
        busyDlg.addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                //closeDialog(evt);
            }
        });

        jPanel1.setBackground(new java.awt.Color(178, 99, 164));
        jPanel1.setPreferredSize(new Dimension(398, 67));

        captionLabel.setFont(new java.awt.Font("Segoe UI", 0, 18)); // NOI18N
        captionLabel.setForeground(new java.awt.Color(255, 255, 255));

        BusyIcon.setForeground(new java.awt.Color(255, 255, 255));
        BusyIcon.setIcon(new ImageIcon(getClass().getResource("BusyRingWhite.gif")));
        BusyIcon.setBusy(true);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
                jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(jPanel1Layout.createSequentialGroup()
                                .addGap(56, 56, 56)
                                .addComponent(BusyIcon, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(captionLabel)
                                .addContainerGap(56, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
                jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                                .addContainerGap(30, Short.MAX_VALUE)
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                        .addComponent(BusyIcon, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addComponent(captionLabel))
                                .addContainerGap())
        );

        DoneButton.setText("Готово");
        DoneButton.setToolTipText("");
        DoneButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                busyDlg.setVisible(false);
                busyDlg.dispose();
            }
        });
        DoneButton.setEnabled(false);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(busyDlg.getContentPane());
        busyDlg.getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGroup(layout.createSequentialGroup()
                                .addGap(57, 57, 57)
                                .addComponent(DescriptionLabel))
                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                .addContainerGap()
                                .addComponent(DoneButton)
                                .addGap(56, 56, 56))
        );
        layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createSequentialGroup()
                                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(18, 18, 18)
                                .addComponent(DescriptionLabel)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 33, Short.MAX_VALUE)
                                .addComponent(DoneButton)
                                .addGap(30, 30, 30))
        );

        busyDlg.setResizable(false);
        busyDlg.pack();
    }
    //endregion

}
